package mindshare;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import info.androidhive.slidingmenu.R;
import android.app.ActionBar;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class SearchFriendsActivity extends Activity {
	private MenuItem refreshMenuItem;
	private String query;
	private List<FriendRequest> friendList = new ArrayList<FriendRequest>();
	List<NameValuePair> params = new ArrayList<NameValuePair>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search_results);

		ActionBar actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);

		handleIntent(getIntent());
	}

	@Override
	protected void onNewIntent(Intent intent) {
		setIntent(intent);
		handleIntent(intent);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.empty_menu, menu);
		refreshMenuItem = menu.findItem(R.id.empty_sea);
		params.clear();
		params.add(new BasicNameValuePair("nickname",query));
		new SearchFriends(this, params, "friends").execute();
		return true;
		
	}
	
	private class SearchFriends extends LoadContent {
		public SearchFriends(Context context, List<NameValuePair> param, String data) {
			super(context, param, data);
		}

		@Override
		protected void onPreExecute() {
			refreshMenuItem.setActionView(R.layout.action_progressbar);
			refreshMenuItem.expandActionView();
		}

		@Override
		protected String doInBackground(String... params) {
			JSONObject json = jParser.makeHttpRequest(url + "obtainFriends.php", param);
			JSONArray friends;
			friendList.clear();
			try {
				friends = json.getJSONArray(data);
				for (int i = 0; i < friends.length(); i++) {
					JSONArray friendsLoc = friends.getJSONArray(i);
					friendList.add(new FriendRequest(friendsLoc.getString(0), friendsLoc.getString(1), friendsLoc.getString(2)));
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			ListView lvSearchFriends = (ListView) ((Activity) c) .findViewById(R.id.lvSearchFriends);
			lvSearchFriends.setAdapter(new FriendsListAdapter(c, R.layout.friendtable_add,friendList, refreshMenuItem));
			lvSearchFriends.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {
//					FriendRequest clickedFriend = friendList.get(position);
//					String message = "You clicked position " + position + " Which name is " + clickedFriend.getUserName();
//					Toast.makeText(c, message, Toast.LENGTH_LONG).show();
					Log.d("MESSAGE", "..................");
//					Log.d("MESSAGE", message);
				}
			});
			
			refreshMenuItem.collapseActionView();
			refreshMenuItem.setActionView(null);
		}
	};

	
	private void handleIntent(Intent intent) {
		if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
			query = intent.getStringExtra(SearchManager.QUERY);
		}
	}
}
